package cn.edu.scau.vsac.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

@Data
public class ResultData<T> {

    // 结果状态码
    private String code;

    // 响应信息
    private String msg;

    // 响应数据
    private T data;

    // 接口请求时间
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timestamp;

    public ResultData() {
        this.timestamp = new Date();
    }

    public static <T> ResultData<T> success(T data) {
        ResultData resultData = new ResultData();
        resultData.setCode(Constants.ResponseCode.SUCCESS.getCode());
        resultData.setMsg(Constants.ResponseCode.SUCCESS.getInfo());
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> fail(Constants.ResponseCode resultCode) {
        ResultData resultData = new ResultData();
        resultData.setCode(resultCode.getCode());
        resultData.setMsg(resultCode.getInfo());
        return resultData;
    }

    public static <T> ResultData<T> fail(Constants.ResponseCode resultCode, String msg) {
        ResultData resultData = new ResultData();
        resultData.setCode(resultCode.getCode());
        resultData.setMsg(msg);
        return resultData;
    }
}
